package com.systop.asset.service.impl;

import java.util.List;
import java.util.random.RandomGenerator;

import com.systop.asset.entity.Asset;
import com.systop.asset.entity.DisposalDetail;
import com.systop.asset.mapper.AssetMapper;
import com.systop.asset.utils.RandomCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.systop.asset.mapper.DisposalMapper;
import com.systop.asset.entity.Disposal;
import com.systop.asset.service.IDisposalService;

/**
 * 处置单Service业务层处理
 * 
 * @author mc
 * @date 2025-07-03
 */
@Service
public class DisposalServiceImpl implements IDisposalService
{
    @Autowired
    private DisposalMapper disposalMapper;
    @Autowired
    private AssetMapper assetMapper;

    /**
     * 查询处置单
     * 
     * @param id 处置单主键
     * @return 处置单
     */
    @Override
    public Disposal selectDisposalById(Long id)
    {
        return disposalMapper.selectDisposalById(id);
    }

    /**
     * 查询处置单列表
     * 
     * @param disposal 处置单
     * @return 处置单
     */
    @Override
    public List<Disposal> selectDisposalList(Disposal disposal)
    {
        return disposalMapper.selectDisposalList(disposal);
    }

    /**
     * 新增处置单
     * 
     * @param disposal 处置单
     * @return 结果
     */
    @Override
    public int insertDisposal(Disposal disposal)
    {
        List<DisposalDetail> detailList = disposal.getAssets();
        if(detailList != null && detailList.size() > 0){
            //TODO: 设置操作人ID 暂时模拟
            disposal.setOperator(5L);
            disposal.setStatus(1);
            //设置处置单编号
            disposal.setOrderNo(RandomCodeGenerator.generateDisposalCode());
            //新增处置单表数据
            disposalMapper.insertDisposal(disposal);
            //新增处置单明细表数据
            detailList.forEach(detail -> {
                detail.setDisposalId(disposal.getId());
                detail.setAssetId(detail.getId());
            });
            disposalMapper.insertDisposalDetailBatch(detailList);

            for(DisposalDetail detail : detailList){
                Asset asset = new Asset();
                asset.setId(detail.getAssetId());
                asset.setStatus(4);
                assetMapper.updateAsset(asset);
            }
            return 1;
        }else{
            return 0;
        }
    }



    /**
     * 修改处置单
     * 
     * @param disposal 处置单
     * @return 结果
     */
    @Override
    public int updateDisposal(Disposal disposal)
    {
        return disposalMapper.updateDisposal(disposal);
    }

    /**
     * 批量删除处置单
     * 
     * @param ids 需要删除的处置单主键
     * @return 结果
     */
    @Override
    public int deleteDisposalByIds(Long[] ids)
    {
        return disposalMapper.deleteDisposalByIds(ids);
    }

    /**
     * 删除处置单信息
     * 
     * @param id 处置单主键
     * @return 结果
     */
    @Override
    public int deleteDisposalById(Long id)
    {
        return disposalMapper.deleteDisposalById(id);
    }
    /**
     * 查询处置单详情
     * @param id
     * @return
     */
    @Override
    public Disposal selectDisposalDetail(String id) {
        return disposalMapper.selectDisposalDetail(id);
    }
}
